package com.zzdx.deployment.web.controller.system;

import java.util.Date;
import java.util.List;

import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.system.domain.Company;
import com.zzdx.deployment.system.domain.SysUser;
import com.zzdx.deployment.system.mapper.CompanyMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.system.domain.Pc;
import com.zzdx.deployment.system.service.IPcService;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.utils.ExcelUtil;

/**
 * 批次 信息操作处理
 * 
 * @author zzdx
 * @date 2020-03-08
 */
@Controller
@RequestMapping("/system/pc")
public class PcController extends BaseController
{
    private String prefix = "system/pc";
	
	@Autowired
	private IPcService pcService;

	@Autowired
	private CompanyMapper companyMapper;
	@RequiresPermissions("system:pc:view")
	@GetMapping()
	public String pc( ModelMap mmap)
	{
		SysUser sysUser = ShiroUtils.getSysUser();
		Integer userId = Integer.valueOf(String.valueOf(sysUser.getUserId()));
		Company company = companyMapper.selectCompanyByUSerId(userId);
		if(company==null){
			mmap.put("companyPhone",sysUser.getPhonenumber());
		   return "/system/company/add";
		}
		mmap.put("company",company);
	    return prefix + "/pc";
	}

	@GetMapping("/pc/{commpanyId}")
	public String companyPc(@PathVariable("companyId") Integer companyId, ModelMap mmap )
	{
		Pc pc = new Pc();
		pc.setCompanyId(companyId);
		mmap.put("pc", pc);
		return prefix + "/pc";
	}


	/**
	 * 查询批次列表
	 */
//	@RequiresPermissions("system:pc:list")
//	@PostMapping("/list")
//	@ResponseBody
//	public TableDataInfo list(Pc pc)
//	{
//		startPage();
//        List<Pc> list = pcService.selectPcList(pc);
//		return getDataTable(list);
//	}

	@RequiresPermissions("system:pc:list")
	@PostMapping("/list/{companyId}")
	@ResponseBody
	public TableDataInfo list(@PathVariable("companyId") Integer companyId)
	{
		Pc pc = new Pc();
		pc.setCompanyId(companyId);
		startPage();
		List<Pc> list = pcService.selectPcList(pc);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出批次列表
	 */
	@RequiresPermissions("system:pc:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Pc pc)
    {
    	List<Pc> list = pcService.selectPcList(pc);
        ExcelUtil<Pc> util = new ExcelUtil<Pc>(Pc.class);
        return util.exportExcel(list, "pc");
    }
	
	/**
	 * 新增批次
	 */
	@GetMapping("/add/{companyId}")
	public String add(@PathVariable("companyId") Integer companyId, ModelMap mmap)
	{
		Pc pc = new Pc();
		pc.setCompanyId(companyId);

		mmap.put("pc", pc);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存批次
	 */
	@RequiresPermissions("system:pc:add")
	@Log(title = "批次", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Pc pc)
	{
		pc.setApplyStatusOne("未提交");
		pc.setApplyStatusTwo("未提交");
		pc.setCreateTime(new Date());
		return toAjax(pcService.insertPc(pc));
	}

	/**
	 * 修改批次
	 */
	@GetMapping("/edit/{pcId}")
	public String edit(@PathVariable("pcId") Integer pcId, ModelMap mmap)
	{
		Pc pc = pcService.selectPcById(pcId);
		mmap.put("pc", pc);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存批次
	 */
	@RequiresPermissions("system:pc:edit")
	@Log(title = "批次", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Pc pc)
	{
		return toAjax(pcService.updatePc(pc));
	}
	
	/**
	 * 删除批次
	 */
	@RequiresPermissions("system:pc:remove")
	@Log(title = "批次", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
 		Pc pc= pcService.selectPcById(Integer.parseInt(ids));
		return toAjax(pcService.deletePcByIds(ids));
	}
	
}
